package cn.it.trigger.http;

import cn.it.infrastructure.api.ITypeApi;
import cn.it.infrastructure.api.dto.GoodsServiceResponseDTO;
import cn.it.infrastructure.api.dto.TypeServiceRequestDTO;
import cn.it.infrastructure.api.dto.TypeServiceResponseDTO;
import cn.it.infrastructure.domain.service.ITypeService;
import cn.it.infrastructure.domain.valobj.ProductSortVO;
import cn.it.infrastructure.po.TbGoods;
import cn.it.infrastructure.po.TbType;
import cn.it.infrastructure.type.enums.ResponseCode;
import cn.it.infrastructure.type.exception.AppException;
import cn.it.infrastructure.type.model.Response;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Mzq
 * @description
 * @createTime 2024/7/5
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/api/${app.config.api-version}/type")
public class TypeApiController implements ITypeApi {

    private ITypeService typeService;

    public TypeApiController(ITypeService typeService) {
        this.typeService = typeService;
    }

    /**
     * 商品分类查询
     * @return
     *
     * curl --request GET \
     *   --url http://localhost:8000/api/v1/type/sort
     *
     */
    @RequestMapping(value = "sort", method = RequestMethod.GET)
    @Override
    public Response<List<TypeServiceResponseDTO>> sort() {
        try {
            List<ProductSortVO> sortVOList = typeService.productSort();
            List<TypeServiceResponseDTO> responseList = new ArrayList<>(sortVOList.size());
            for (ProductSortVO sortVO : sortVOList) {
                TypeServiceResponseDTO response = TypeServiceResponseDTO.builder()
                        .id(sortVO.getId())
                        .name(sortVO.getName())
                        .description(null)
                        .businessId(sortVO.getBusinessId())
                        .build();
                responseList.add(response);
            }
            log.info("查询分类成功");
            return Response.<List<TypeServiceResponseDTO>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(responseList)
                    .build();
        } catch (AppException e) {
            log.error("登录失败");

            return Response.<List<TypeServiceResponseDTO>>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }

    }

    @RequestMapping(value = "category", method = RequestMethod.POST)
    @Override
    public Response<List<GoodsServiceResponseDTO>> queryTypeGoods(@RequestBody TypeServiceRequestDTO requestDTO) {
        try {
            Long businessId = requestDTO.getBusinessId();

            List<TbGoods> goods = typeService.queryGoodsByBusinessId(businessId);

            List<GoodsServiceResponseDTO> responseList = new ArrayList<>(goods.size());
            for (TbGoods good : goods) {
                GoodsServiceResponseDTO response = GoodsServiceResponseDTO.builder()
                                .name(good.getName())
                                .realPrice(good.getRealPrice())
                                .discountPrice(good.getDiscountPrice())
                                .imageUrl(good.getImageUrl())
                                .norms(good.getNorms())
                                .stock(good.getStock())
                                .description(good.getDescription())
                                .isShow(good.getIsShow())
                                .build();
                responseList.add(response);
            }
            return Response.<List<GoodsServiceResponseDTO>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(responseList)
                    .build();
        } catch (AppException e) {

            return Response.<List<GoodsServiceResponseDTO>>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }

    }
}
